<?php
//会员列表
if($do=='member_list'){
	check_permissions('member_read');
	$member_list=array();
	$sql="SELECT * FROM ".$db_prefix."member";
	$page_size=30;
	$page_current=isset($_GET['page'])?intval($_GET['page']):1;
	$count=$GLOBALS['db']->getcount($sql);
	$res=$GLOBALS['db']->getall($sql." order by member_id desc limit ".(($page_current-1)*$page_size).",".$page_size);
	if($count>0){
			$no=$count-(($page_current-1)*$page_size);
			foreach($res as $row){
				$member_list[$row['member_id']]['no']=$no;
				$member_list[$row['member_id']]['id']=$row['member_id'];
				$member_list[$row['member_id']]['mail']=$row['member_mail'];
				$member_list[$row['member_id']]['nickname']=$row['member_nickname'];
				$member_list[$row['member_id']]['join_time']=date("Y-m-d h:i:s",$row['member_join_time']);
				$no--;
			}
			$pagebar=pagebar(get_self(),"action=member&do=member_list&",$page_current,$page_size,$count);
	}else{
			$pagebar="";
	}
	$smarty=new smarty();smarty_header();
	$smarty->assign('member_list',$member_list);
	$smarty->assign('pagebar',$pagebar);
	$smarty->display('member_list.htm');
}
//添加会员
if($do=='member_add'){
	check_permissions('member_write');
	$member=array();
	$member['id']=0;
	$member['nickname']='';
	$member['mail']='';
	$member['password']='';
	$member['safecode']='';
	$member['group_id']=0;
	$member['name']='';
	$member['sex']=0;
	$member['birthday']=date('Y-m-d');
	$member['phone']='';
	$member['photo']='';
	$member['from']='';
	$member['other']='';
	$member['join_time']='';
	$member['last_time']='';
	$member['last_ip']='';
	$member['validation']=1;
	$member['state']=1;
	$smarty=new smarty();smarty_header();
	$smarty->assign('member',$member);
	$smarty->assign('member_group',get_group_list());
	$smarty->assign('mode','insert');
	$smarty->display('member_info.htm');
}
//插入会员
if($do=='member_insert'){
	check_permissions('member_write');
	$member_nickname=empty($_POST['member_nickname'])?'':addslashes(trim($_POST['member_nickname']));
	$member_mail=empty($_POST['member_mail'])?'':addslashes(trim($_POST['member_mail']));
	$member_password=empty($_POST['member_password'])?'':addslashes(trim($_POST['member_password']));
	$member_safecode=empty($_POST['member_safecode'])?'':addslashes(trim($_POST['member_safecode']));
	$member_name=empty($_POST['member_name'])?'':addslashes(trim($_POST['member_name']));
	$member_sex=empty($_POST['member_sex'])?0:intval($_POST['member_sex']);
	$member_other=empty($_POST['member_other'])?'':addslashes(trim($_POST['member_other']));
	$member_phone=empty($_POST['member_phone'])?'':addslashes(trim($_POST['member_phone']));
	$member_from=empty($_POST['member_from'])?'':addslashes(trim($_POST['member_from']));
	$member_birthday=empty($_POST['member_birthday'])?0:strtotime($_POST['member_birthday']);
	$member_state=empty($_POST['member_state'])?0:intval($_POST['member_state']);
	$group_id=empty($_POST['group_id'])?0:intval($_POST['group_id']);
	$member_validation=empty($_POST['member_validation'])?0:intval($_POST['member_validation']);
	if(empty($member_nickname)){
		message(array('text'=>'昵称不能为空！','link'=>''));
	}
	if(empty($member_mail)){
		message(array('text'=>'邮箱地址不能为空！','link'=>''));
	}
	if(!is_email($member_mail)){
		message(array('text'=>'邮箱地址不合法！','link'=>''));
	}
	$member_photo=upload($_FILES['member_photo']);
	$insert=array();
	$insert['member_mail']=$member_mail;
	$insert['member_password']=password($member_password);
	$insert['member_safecode']=password($member_safecode);
	$insert['member_nickname']=$member_nickname;
	$insert['member_name']=$member_name;
	$insert['member_sex']=$member_sex;
	$insert['member_phone']=$member_phone;
	$insert['member_photo']=$member_photo;
	$insert['member_birthday']=$member_birthday;
	$insert['member_other']=$member_other;
	$insert['member_from']=$member_from;
	$insert['member_state']=$member_state;
	$insert['group_id']=$group_id;
	$insert['member_join_time']=$_SERVER['REQUEST_TIME'];
	$insert['member_last_time']=$_SERVER['REQUEST_TIME'];
	$insert['member_last_ip']=get_ip();
	$insert['member_validation']=$member_validation;
	$insert['member_validation_key']='';
	//print_r($insert);exit;
	$db->insert($db_prefix."member",$insert);
	admin_log('insert','member',$member_mail);
	clear_cache();
	message(array('text'=>'添加会员成功！','link'=>'?action=member&do=member_list'));
}
//会员编辑
if($do=='member_edit'){
	check_permissions('member_write');
	$member_id=empty($_GET['member_id'])?'':intval($_GET['member_id']);
	$row=$db->getone("SELECT * FROM ".$db_prefix."member WHERE member_id='$member_id'");
	$member=array();
	$member['id']=$row['member_id'];
	$member['nickname']=$row['member_nickname'];
	$member['mail']=$row['member_mail'];
	$member['password']=$row['member_password'];
	$member['safecode']=$row['member_safecode'];
	$member['group_id']=$row['group_id'];
	$member['name']=$row['member_name'];
	$member['sex']=$row['member_sex'];
	$member['birthday']=date('Y-m-d',$row['member_birthday']);
	$member['phone']=$row['member_phone'];
	$member['photo']=$row['member_photo'];
	$member['from']=$row['member_from'];
	$member['other']=$row['member_other'];
	$member['join_time']=date('Y-m-d',$row['member_join_time']);
	$member['last_time']=date('Y-m-d',$row['member_last_time']);
	$member['last_ip']=$row['member_last_ip'];
	$member['validation']=$row['member_validation'];
	$member['state']=$row['member_state'];
	$smarty=new smarty();smarty_header();
	$smarty->assign('member',$member);
	$smarty->assign('member_group',get_group_list());
	$smarty->assign('mode','update');
	$smarty->display('member_info.htm');
}
//会员更新
if($do=='member_update'){
	check_permissions('member_write');
	$member_id=empty($_POST['member_id'])?'':intval($_POST['member_id']);
	$member_mail=empty($_POST['member_mail'])?'':addslashes(trim($_POST['member_mail']));
	$member_nickname=empty($_POST['member_nickname'])?'':addslashes(trim($_POST['member_nickname']));
	$member_password=empty($_POST['member_password'])?'':addslashes(trim($_POST['member_password']));
	$member_safecode=empty($_POST['member_safecode'])?'':addslashes(trim($_POST['member_safecode']));
	$member_name=empty($_POST['member_name'])?'':addslashes(trim($_POST['member_name']));
	$member_sex=empty($_POST['member_sex'])?'':intval($_POST['member_sex']);
	$member_other=empty($_POST['member_other'])?'':addslashes(trim($_POST['member_other']));
	$member_phone=empty($_POST['member_phone'])?'':addslashes(trim($_POST['member_phone']));
	$member_from=empty($_POST['member_from'])?'':addslashes(trim($_POST['member_from']));
	$member_birthday=empty($_POST['member_birthday'])?'':strtotime($_POST['member_birthday']);
	$member_state=empty($_POST['member_state'])?0:intval($_POST['member_state']);
	$group_id=empty($_POST['group_id'])?0:intval($_POST['group_id']);
	$member_validation=empty($_POST['member_validation'])?0:intval($_POST['member_validation']);
	$member_photo_old=empty($_POST['member_photo_old'])?'':addslashes(trim($_POST['member_photo_old']));
	$member_photo_delete=empty($_POST['member_photo_delete'])?'':addslashes(trim($_POST['member_photo_delete']));
	if(empty($member_nickname)){
		message(array('text'=>'昵称不能为空！','link'=>''));
	}
	$member_photo=upload($_FILES['member_photo']);
	$update=array();
	if(!empty($member_password)){
		$update['member_password']=password($member_password);
	}
	if(!empty($member_safecode)){
		$update['member_safecode']=password($member_safecode);
	}
	$update['member_nickname']=$member_nickname;
	$update['member_name']=$member_name;
	$update['member_sex']=$member_sex;
	$update['member_phone']=$member_phone;
	if(!empty($member_photo)){
		@unlink(ROOT_PATH."/uploads/".$member_photo_old);
		$update['member_photo']=$member_photo;
	}
	if(!empty($member_photo_delete)){
		@unlink(ROOT_PATH."/uploads/".$member_photo_delete);
		$update['member_photo']='';
	}
	$update['member_birthday']=$member_birthday;
	$update['member_other']=$member_other;
	$update['member_from']=$member_from;
	$update['member_state']=$member_state;
	$update['member_validation']=$member_validation;
	$update['group_id']=$group_id;
	//print_r($insert);exit;
	$db->update($db_prefix."member",$update,"member_id=$member_id");
	admin_log('update','member',$member_mail);
	clear_cache();
	message(array('text'=>'更新会员成功！','link'=>'?action=member&do=member_list'));
}
//会员删除
if($do=='member_delete'){
	check_permissions('member_delete');
	$member_id=empty($_POST['member_id'])?array():$_POST['member_id'];
	if(count($member_id)>0){
		foreach($member_id as $value){
			$row=$db->getone("SELECT member_photo FROM ".$db_prefix."member WHERE member_id=$value");
			if(!empty($row['member_photo'])){
				@unlink(ROOT_PATH."/uploads/".$row['member_photo']);
			}
			$db->delete($db_prefix."member","member_id=$value");
		}
	}
	admin_log('delete','member','');
	clear_cache();
	message(array('text'=>'删除会员成功！','link'=>'?action=member&do=member_list'));
}
//会员组列表
if($do=='group_list'){
	check_permissions('member_group_read');
	$group_list=array();
	$res=$db->getall("SELECT * FROM ".$db_prefix."member_group order by group_id asc");
	if($res){
		foreach($res as $row){
			$group_list[$row['group_id']]['name']=$row['group_name'];
			$group_list[$row['group_id']]['id']=$row['group_id'];
		}
	}
	$smarty=new smarty();smarty_header();
	$smarty->assign('group_list',$group_list);
	$smarty->display('member_group_list.htm');
}
//会员组添加
if($do=='group_add'){
	check_permissions('member_group_write');
	$group=array();
	$group['id']=0;
	$group['name']='';
	$smarty=new smarty();smarty_header();
	$smarty->assign('group',$group);
	$smarty->assign('mode','insert');
	$smarty->display('member_group_info.htm');
}
//会员组插入
if($do=='group_insert'){
	check_permissions('member_group_write');
	$group_name=empty($_POST['group_name'])?'':addslashes(trim($_POST['group_name']));
	if(empty($group_name)){
		message(array('text'=>'会员组名称不能为空！','link'=>''));
	}
	$insert=array();
	$insert['group_name']=$group_name;
	$db->insert($db_prefix."member_group",$insert);
	admin_log('insert','member_group',$group_name);
	clear_cache();
	message(array('text'=>'添加会员组成功！','link'=>'?action=member&do=group_list'));
}
//会员组编辑
if($do=='group_edit'){
	check_permissions('member_group_write');
	$group_id=empty($_GET['group_id'])?'':intval($_GET['group_id']);
	$row=$db->getone("SELECT * FROM ".$db_prefix."member_group  WHERE group_id='$group_id'");
	$group=array();
	$group['id']=$row['group_id'];
	$group['name']=$row['group_name'];
	$smarty=new smarty();smarty_header();
	$smarty->assign('group',$group);
	$smarty->assign('mode','update');
	$smarty->display('member_group_info.htm');
}
//会员组更新
if($do=='group_update'){
	check_permissions('member_group_write');
	$group_id=empty($_POST['group_id'])?'':intval($_POST['group_id']);
	$group_name=empty($_POST['group_name'])?'':addslashes(trim($_POST['group_name']));
	if(empty($group_name)){
		message(array('text'=>'会员组名称不能为空！','link'=>''));
	}
	$update=array();
	$update['group_name']=$group_name;
	$db->update($db_prefix."member_group",$update,"group_id=$group_id");
	admin_log('update','member_group',$group_name);
	clear_cache();
	message(array('text'=>'更新会员组成功！','link'=>'?action=member&do=group_list'));
}
//会员组删除
if($do=='group_delete'){
	check_permissions('member_group_delete');
	$group_id=empty($_GET['group_id'])?0:intval($_GET['group_id']);
	$group_name=get_group_name($group_id);
	$db->delete($db_prefix."member_group","group_id=$group_id");
	admin_log('delete','member_group',$group_name);
	clear_cache();
	message(array('text'=>'删除会员组成功！','link'=>'?action=member&do=group_list'));
}
?>